//
//  SDShopingVC.m
//  SDHuiYuShoping
//
//  Created by luojiao on 2017/4/12.
//  Copyright © 2017年 luojiao. All rights reserved.
//

#import "SDShopingVC.h"
#import "OrderDetailVC.h"

@interface SDShopingVC ()

{
    UIButton *sectionButton;
    NSArray *dataArr;
    NSInteger productNum;
}

@end

@implementation SDShopingVC

- (void)viewDidLoad {
    [super viewDidLoad];

    productNum = 1;
    self.navigationController.navigationBar.hidden = YES;
    //注册headerfooterView
    [_tableView registerClass:[HeaderFooterViewe class] forHeaderFooterViewReuseIdentifier:@"headerView"];
    //隐藏tableView的原生线条
    self.tableView.separatorStyle = NO;
    self.emptyBgImage.image = [UIImage imageNamed:@"shopping_cart"];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //显示tabbar
    self.tabBarController.tabBar.hidden = NO;
    //设置状态栏颜色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    //设置tabbar底部的商品数量
    [[[[[self tabBarController] viewControllers] objectAtIndex:1] tabBarItem] setBadgeValue:@"2"];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 162;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 32;
}

//设置每个sectionfooter的高度 无限接近零
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShoppingCarCell *cell;
    static NSString *cellIdentifier = @"shoppingCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ShoppingCarCell" owner:self options:nil][0];
    }
    cell.delegate = self;
    cell.textField.delegate = self;
    
    //cell 没有选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderFooterViewe *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    NSString *titleStr = @"VIVO手机旗舰店";
    [headerView.sectionButton setTitle:titleStr forState:UIControlStateNormal];
    [headerView.sectionButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -100, 0, 0)];
    headerView.sectionButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [headerView.sectionButton setTitleColor:[FRUtils colorWithHexString:@"323232"] forState:UIControlStateNormal];
    headerView.delegate = self;

    return headerView;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductViewController *productVC = [[ProductViewController alloc] init];
    [self.navigationController pushViewController:productVC animated:YES];
}





#pragma mark - buttonClick
- (IBAction)edtorButton:(id)sender
{
    
}
- (IBAction)payforButton:(id)sender
{
    OrderDetailVC *orderDetailVC = [[OrderDetailVC alloc] init];
    [self.navigationController pushViewController:orderDetailVC animated:YES];
}

#pragma mark - HeaderViewDelegate

//section 上的选中button
- (void)headerViewButtonClick:(HeaderFooterViewe *)headerFooterView
{
    
    headerFooterView.sectionButton.selected =! headerFooterView.sectionButton.selected;
    
    if (headerFooterView.sectionButton.selected)
    {
        [headerFooterView.sectionButton setImage:[UIImage imageNamed:@"select_btn_sel"] forState:UIControlStateNormal];
    }
    else
    {
         [headerFooterView.sectionButton setImage:[UIImage imageNamed:@"select_btn"] forState:UIControlStateNormal];
    }
}


#pragma mark - ShoppingDelegate

//cell上的选择按钮
-(void)chooseButton:(ShoppingCarCell *)cell
{
    cell.choseButton.selected =! cell.choseButton.selected;
    if (cell.choseButton.selected)
    {
        [cell.choseButton setImage:[UIImage imageNamed:@"select_btn_sel"] forState:UIControlStateNormal];
    }
    else
    {
        [cell.choseButton setImage:[UIImage imageNamed:@"select_btn"] forState:UIControlStateNormal];
    }
}


- (void)subButtonClick:(ShoppingCarCell *)cell
{
    productNum --;
    NSString *productStr = [NSString stringWithFormat:@"%ld",productNum];
    cell.textField.text = productStr;

}

- (void)addButtonClick:(ShoppingCarCell *)cell
{
    productNum ++;
    NSString *productStr = [NSString stringWithFormat:@"%ld",productNum];
    cell.textField.text = productStr;

}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
